NO_COLOR=\033[0m
OK_COLOR=\033[32;01m
ERROR_COLOR=\033[31;01m

CGNSDIR = ..
include $(CGNSDIR)/make.defs

CGNSLIB = $(CGNSDIR)/$(LIBCGNS)

COPTS  = $(CFLAGS) -I$(CGNSDIR) $(MPIINC) @BUILDOPTS@
FOPTS  = $(FFLAGS) -I$(CGNSDIR) $(MPIINC)
LDLIBS = $(CGNSLIB) $(BUILDLIBS)

#----------

CALL =	pcgns_ctest$(EXE) \
	cexample$(EXE) \
	benchmark$(EXE) \
	benchmark_hdf5$(EXE) \
	open_close$(EXE) \
        comm_test$(EXE) \
	test_base$(EXE) \
	test_unstructured$(EXE) \
	test_unstruc_quad$(EXE) \
	test_zone$(EXE) \
	test_general_readwrite$(EXE) \
	thesis_benchmark$(EXE)

FALL =	pcgns_ftest$(EXE) ftest_zone$(EXE) fexample$(EXE) benchmark_hdf5_f90$(EXE) 

ALL   = $(CALL) @FALL@
TESTS = $(ALL)

all : $(ALL)

#----------

test : $(TESTS)
	@echo " "; \
	status=0; \
	echo "=== running parallel tests ==="; \
	 for x in $(TESTS) ; do \
	   if `$(MPIEXEC) ./$$x >/dev/null 2>&1` ; then \
	     printf "$$x ... $(OK_COLOR) PASSED $(NO_COLOR) \n"; \
	   else \
	     printf "$$x ... $(ERROR_COLOR) *** FAILED *** $(NO_COLOR) \n"; \
		status=1; \
	   fi; \
	 done; \
	 echo "=== finished ==="; \
         if test $$status == 1; then \
           printf "$(ERROR_COLOR)"; \
         else \
           printf "$(OK_COLOR)"; \
         fi; \
         printf "$$status tests failed $(NO_COLOR) \n"; \
	 exit $$status;

#----------

pcgns_ctest$(EXE) : pcgns_ctest.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ pcgns_ctest.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

cexample$(EXE) : cexample.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ cexample.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

benchmark$(EXE) : benchmark.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ benchmark.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

benchmark_hdf5$(EXE) : benchmark_hdf5.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ benchmark_hdf5.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

open_close$(EXE) : open_close.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ open_close.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

comm_test$(EXE) : comm_test.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ comm_test.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_base$(EXE) : test_base.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_base.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_unstructured$(EXE) : test_unstructured.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_unstructured.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_unstruc_quad$(EXE) : test_unstruc_quad.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_unstruc_quad.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_zone$(EXE) : test_zone.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_zone.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@


#----------

test_general_readwrite$(EXE) : test_general_readwrite.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_general_readwrite.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

thesis_benchmark$(EXE) : thesis_benchmark.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ thesis_benchmark.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

pcgns_ftest$(EXE) : pcgns_ftest.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ pcgns_ftest.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

ftest_zone$(EXE) : ftest_zone.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ ftest_zone.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

fexample$(EXE) : fexample.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ fexample.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

benchmark_hdf5_f90$(EXE) : benchmark_hdf5_f90.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ benchmark_hdf5_f90.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

clean :
	-$(RM) *.$(O) *.mod *.MOD

allclean : clean
	-$(RM) $(ALL)
	-$(RM) *~ *.bak
	-$(RM) *.cgns *.cg?

